package com.wxp.demo05.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.wxp.demo05.R;

import java.util.Random;

/**
 * 5.2 找回密码
 */
public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener {

    private String mPhone;
    private String mVerifyCode;
    private EditText et_password_first;
    private EditText et_password_second;
    private EditText et_verifycode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login_forget);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        this.setTitle("5.2 找回密码");

        //获取传入的手机号码
        Intent intent = this.getIntent();
        mPhone = intent.getStringExtra("phone");
        et_password_first = findViewById(R.id.et_password_first);
        et_password_second = findViewById(R.id.et_password_second);
        et_verifycode = findViewById(R.id.et_verifycode);

        this.findViewById(R.id.btn_verifycode).setOnClickListener(this);
        this.findViewById(R.id.btn_confirm).setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_verifycode) {
            // 点击了“获取验证码”按钮
            // 生成六位随机数字的验证码
            mVerifyCode = String.format("%06d", new Random().nextInt(999999));
            // 以下弹出提醒对话框，提示用户记住六位验证码数字
            AlertDialog.Builder buider = new AlertDialog.Builder(this);
            buider.setTitle("请记住验证码");
            buider.setMessage("手机号" + mPhone + ",本次验证码是" + mVerifyCode + ",请输入验证码");
            buider.setPositiveButton("好的", null);
            AlertDialog dialog = buider.create();
            dialog.show();
        } else if (v.getId() == R.id.btn_confirm) {
            // 点击了“确定”按钮
            String password_first = et_password_first.getText().toString();
            String password_second = et_password_second.getText().toString();
            if (password_first.length() != this.getResources().getInteger(R.integer.password_max_length)) {
                Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!password_first.equals(password_second)) {
                Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!mVerifyCode.equals(et_verifycode.getText().toString())) {
                Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
                return;
            }

            Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
            // 以下把修改好的新密码返回给上一个页面
            Intent intent = new Intent();
            intent.putExtra("new_password", password_first);
            setResult(Activity.RESULT_OK, intent);
            finish();
        }
    }
}